package com.koocloud.electroplatemanage.auth.service.impl;


import com.koocloud.electroplatemanage.auth.mapper.UserMapper;
import com.koocloud.electroplatemanage.auth.service.UserService;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.pojo.SysCombo;
import com.koocloud.electroplatemanage.pojo.User;
import com.koocloud.electroplatemanage.utils.ListUtil;
import com.koocloud.electroplatemanage.utils.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public ResponseTemplate selectUserInfoList(String userName, int page, int limit) {
        List<User> list = userMapper.selectUserInfoList(userName);
        System.out.println(list);
        // 开始行数
        int startRowNo = (((page <= 0 ? 1 : page) - 1) * limit) + 1;
        // 执行行数
        int tempRowCount = 0;
        // 返回结果的行
        SysCombo resultRow = null;
        // 总行数
        int allRowCount = 0;

        List<SysCombo> resultList = new ArrayList<SysCombo>();

        if (ListUtil.isEmpty(list)) {
            // 返回没数据
            return ResponseTemplate.builder().code(0).message("").count(Long.valueOf(0)).data(null).build();
        } else {
            // 循环下拉菜单
            for (User user : list) {
                System.out.println(user.getClass());
                // 每循环一次，总行数+1
                allRowCount++;
                // 如果总行数等于开始行数
                if (allRowCount >= startRowNo && tempRowCount < limit) {
                    tempRowCount++;
                    // 结果行
                    resultRow = new SysCombo();
                    PojoUtil.copy(user, resultRow);
                    // 返回列表中添加一行
                    resultList.add(resultRow);
                }
            }
            if (ListUtil.isEmpty(resultList)) {
                return ResponseTemplate.builder().code(1).message("数据不存在").count(Long.valueOf(0)).data(null).build();
            } else {
                return ResponseTemplate.builder().code(0).message("").count(Long.valueOf(allRowCount)).data(resultList).build();
            }
        }
    }

    @Override
    public User selectUserByName(String userName) {
        return userMapper.selectUserByName(userName);
    }

    @Override
    public ResponseTemplate selectAllUserInfo(int page, int limit) {
        List<User> list = userMapper.selectAllUserInfo();
        System.out.println(list);
        // 开始行数
        int startRowNo = (((page <= 0 ? 1 : page) - 1) * limit) + 1;
        // 执行行数
        int tempRowCount = 0;
        // 返回结果的行
        User resultRow = null;
        // 总行数
        int allRowCount = 0;

        List<User> resultList = new ArrayList<User>();

        if (ListUtil.isEmpty(list)) {
            // 返回没数据
            return ResponseTemplate.builder().code(0).message("").count(Long.valueOf(0)).data(null).build();
        } else {
            // 循环下拉菜单
            for (User  user : list) {
                System.out.println(user.getClass());
                // 每循环一次，总行数+1
                allRowCount++;
                // 如果总行数等于开始行数
                if (allRowCount >= startRowNo && tempRowCount < limit) {
                    tempRowCount++;
                    // 结果行
                    resultRow = new User();
                    PojoUtil.copy(user, resultRow);
                    // 返回列表中添加一行
                    resultList.add(resultRow);
                }
            }
            if (ListUtil.isEmpty(resultList)) {
                return ResponseTemplate.builder().code(1).message("数据不存在").count(Long.valueOf(0)).data(null).build();
            } else {
                return ResponseTemplate.builder().code(0).message("").count(Long.valueOf(allRowCount)).data(resultList).build();
            }
        }
    }

    @Override
    public int insertUserInfo(User user) {

        List<User> list = userMapper.checkUserInfo(user);
        if (ListUtil.isEmpty(list)) {
            return userMapper.insertUserInfo(user);
        } else {
            return 0;
        }
    }

    @Override
    public int updateUserInfo(User user) {
        List<User> list = userMapper.checkUserInfo(user);
        if (ListUtil.isEmpty(list)) {
            return 0;
        } else {
            return userMapper.updateUserInfo(user);
        }

    }

    @Override
    public int deleteUserInfo(int userId) {
        User user = new User();
        user.setId(userId);
        List<User> list = userMapper.checkUserInfo(user);
        if (ListUtil.isEmpty(list)) {
            return 0;
        } else {
            return userMapper.deleteUserInfo(userId);
        }
    }
    }

